package com.huan.social.security;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.social.security.SpringSocialConfigurer;

/**
 * spring security 的配置
 *
 * @author huan.fu
 * @date 2018/9/27 - 17:55
 */
@EnableWebSecurity
public class SpringSecurityConfiguration extends WebSecurityConfigurerAdapter {

	@Autowired
	private UserDetailsService userDetailsService;

	@Autowired
	private SpringSocialConfigurer springSocialConfigurer;

	@Override
	protected void configure(AuthenticationManagerBuilder auth) throws Exception {
		auth.userDetailsService(userDetailsService);
	}

	@Override
	protected void configure(HttpSecurity http) throws Exception {
		http.formLogin()
				.loginPage("/login")
				.and()
				.authorizeRequests()
				.antMatchers("/login", "/auth/**").permitAll()
				.anyRequest().authenticated()
				.and()
				.sessionManagement()
				.and()
				// 将 spring social 加入到 spring security 的过滤器链中
				.apply(springSocialConfigurer);
	}
}